package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import entity.ParkReceipt;
import util.Constants;
import util.TableData;
import util.TableUtil;
import util.Validator;

public class ParkReceiptJPanel extends JPanel {
	private static final long serialVersionUID = -6222257462001117510L;

	private JTable table;

	private TableData tableData;

	public ParkReceiptJPanel() {
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createTitledBorder("停车账单"));
		initTopPanel();
		initTablePanel();
	}

	private void initTopPanel() {
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));

		JPanel searchPnl = new JPanel();
		searchPnl.setLayout(new BoxLayout(searchPnl, BoxLayout.X_AXIS));

		JPanel inputPnl = new JPanel();
		inputPnl.setLayout(null);

		JLabel lab1 = new JLabel("车牌号", SwingConstants.RIGHT);
		lab1.setFont(Constants.font);
		lab1.setBounds(0, 0, 60, 30);

		JTextField lab1input = new JTextField();
		Validator.bind(lab1input,Validator.CARID);
		lab1input.setBounds(65, 0, 120, 30);

		// 进货单种类下拉框
		// select = new JComboBox();
		// select.addItem(new Item("全部", "全部"));

		inputPnl.add(lab1);
		inputPnl.add(lab1input);

		JPanel btnBar = new JPanel();
		btnBar.setLayout(new BoxLayout(btnBar, BoxLayout.Y_AXIS));

		JButton queryBtn = new JButton("查询");
		queryBtn.setFont(Constants.font);
		queryBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lab1inputStr = lab1input.getText().trim();
				String[] params = { lab1inputStr };
				refreshTable(params);
			}
		});

		JButton resetBtn = new JButton("重置");
		resetBtn.setFont(Constants.font);
		resetBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lab1input.setText("");
			}
		});

		btnBar.add(queryBtn);
		btnBar.add(resetBtn);

		searchPnl.add(inputPnl);
		searchPnl.add(btnBar);

		topPanel.add(searchPnl);
		add(topPanel, BorderLayout.NORTH);
	}

	private void initTablePanel() {
		String columns[] = { "编号", "车牌号", "车位号", "入场时间","离场时间","计费时长","停车费" };
		List<String[]> rows = new ArrayList<>();
		tableData = new TableData(columns, rows);
		table = new JTable(tableData);
		TableUtil.setTableStyle(table);
		JScrollPane jsPane = new JScrollPane(table);
		TableUtil.setJspStyle(jsPane);
		add(jsPane, BorderLayout.CENTER);
	}

	private void refreshTable(String[] params) {
		ParkReceipt parkReceipt = new ParkReceipt();
		List<String[]> rows = parkReceipt.query(params);
		tableData.refresh(rows);
	}
}

